package com.googlecode.garbagecan.springsecuritystudy.user;

import java.util.HashMap;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;

public class UserInfoService implements UserDetailsService {

	private Map<String, UserInfo> userMap = null;

	public UserInfoService() {
		userMap = new HashMap<String, UserInfo>();

		UserInfo userInfo = null;

		userInfo = new UserInfo("admin", "admin", true,
				new GrantedAuthority[]{
					new GrantedAuthorityImpl("ROLE_ADMIN"),
					new GrantedAuthorityImpl("ROLE_USER")
				});
		userInfo.setEmail("admin@kongxx.com");
		userMap.put("admin", userInfo);

		userInfo = new UserInfo("user", "user", true,
				new GrantedAuthority[]{new GrantedAuthorityImpl("ROLE_USER")
				});
		userInfo.setEmail("user@kongxx.com");
		userMap.put("user", userInfo);

		for (int i = 0; i < 10; i++) {
			userInfo = new UserInfo("user" + i, "user" + i, true,
					new GrantedAuthority[]{new GrantedAuthorityImpl("ROLE_USER")
					});
			userInfo.setEmail("user" + i + "@kongxx.com");
			userMap.put("user" + i, userInfo);
		}
	}

	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		return userMap.get(username);
	}
}
